jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.envDef.envDtls.medBus.mesSer.MessageServer",
        jsx3.gui.LayoutGrid,
        null,
        function (servMast) {

            servMast.prototype.mtxList = null;
            servMast.prototype.addMode = false;
            servMast.prototype.environmentName = null;
            servMast.prototype.selection = new jsx3.util.List(0);
            servMast.prototype.entityID = null;

            servMast.prototype.subscribeAll = function() {
                this.mtxList = this.getDescendantOfName("mtx_master_27D");
                this.addMode = false;
                var me = this;
                tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, me, me.initialiseList);
                tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.EMS_CREATE_OVER, me, me.finishedAdd);
                tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.EMS_REFRESH, me, me.onRefresh);
            }

            servMast.prototype.unsubscribeAll = function() {
                var me = this;
                tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.ENVIRONMENT_ROW_SELECTED, me);
                tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.envDef.EventID.EMS_CREATE_OVER, me);
                tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.envDef.EventID.EMS_REFRESH, me);

            }

            servMast.prototype.initialiseList = function(objEvent) {
                if(objEvent && objEvent.environmentName) {
                    this.environmentName = objEvent.environmentName;
                    this.entityID = objEvent.environmentID;
                    this.callgetServiceBusConfigurations();
                }
                else {
                    this.clearAll();
                }
            }

            servMast.prototype.clearAll = function() {
                tibco.admin.Main.getServer().getCache().clearById(this.mtxList.getXMLId());
                this.mtxList.repaint();
                this.getDescendantOfName("btn_add_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                this.getDescendantOfName("btn_remove_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
            }

            servMast.prototype.publishAdd = function() {
                this.getDescendantOfName("btn_add_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                this.getDescendantOfName("btn_remove_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                var cachedTree = this.mtxList.getXML();
                var objParent = cachedTree.getRootNode()
                var record = objParent.createNode(jsx3.xml.Entity.TYPEELEMENT, "record");
                var strID = jsx3.xml.CDF.getKey();
                record.setAttribute("jsxid", strID)
                var numberOfEmsServers = cachedTree.selectNodes("//record").getLength();
                var newNodeName = null;
                while(++numberOfEmsServers) {
                    newNodeName = tibco.admin.Main.getServer().getDynamicProperty("@@newMessagingServer27D") + (numberOfEmsServers);
                    if(!cachedTree.selectSingleNode("//record[@name='" + newNodeName + "']"))
                        break;
                }
                record.setAttribute("name", newNodeName);
                record.setAttribute("providerURL", tibco.admin.Main.getServer().getDynamicProperty("@@default_txtProURL27E"));
                record.setAttribute("subtype", tibco.admin.Main.getServer().getDynamicProperty("@@option_sltConTypeDIRECT27E"));
                objParent.appendChild(record);
                this.mtxList.repaintData();
                this.mtxList.deselectAllRecords();
                this.mtxList.selectRecord(strID);
                this.addMode = true;
                var msg = new Object();
                msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.EMS_CREATE;
                msg.environmentName = this.environmentName;
                msg.environmentID = this.entityID;
                msg.name = record.getAttribute("name");
                msg.providerURL = record.getAttribute("providerURL");
                msg.subtype = record.getAttribute("subtype");
                msg.numberOfServers = numberOfEmsServers;
                tibco.admin.PubSub.publish(msg);
            }

            servMast.prototype.finishedAdd = function(objEvent) {
                this.addMode = false;
                this.getDescendantOfName("btn_add_27D").setEnabled(jsx3.gui.Form.STATEENABLED, true);
                this.getDescendantOfName("btn_remove_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                this.callgetServiceBusConfigurations();
            }


            //call this method to begin the service call (tibco.master.callgetServiceBusConfigurations();)
            servMast.prototype.callgetServiceBusConfigurations = function() {
                if(this.entityID) {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/envDef/envDtls/medBus/mesSer/rules/getServiceBusRules.xml",
                            "getServiceBusConfigurations",
                            "get Service Bus Configurations");
                    //subscribe and call
                    objService.environmentName = this.environmentName;
                    objService.environmentID = this.entityID;
                    var me = this;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.ongetServiceBusConfigurationsSuccess);
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getEnvironmentEndpointURL());
                    objService.doCall();
                }
            };

            servMast.prototype.ongetServiceBusConfigurationsSuccess = function(objEvent) {
                this.getDescendantOfName("btn_add_27D").setEnabled(jsx3.gui.Form.STATEENABLED, true);

                var cacheDoc = this.mtxList.getXML();
                if(cacheDoc) {
                    var flag = false;
                    var iter = this.selection.iterator();
                    while(iter.hasNext()) {
                        var jsxid = iter.next();
                        var record = this.mtxList.getXML().selectSingleNode("//record[@jsxid='" + jsxid + "']");
                        if(record) {
                            flag = true;
                            record.setAttribute("jsxselected", 1);
                            if(!this.multiselected) {
                                var items = new Array();
                                items.push(record.getAttribute("jsxid"));
                                this.publishSelect(items);
                            }
                        }
                    }
                    if(!flag || this.multiselected) {
                        if(!flag)
                            this.getDescendantOfName("btn_remove_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                        var msg = new Object();
                        msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.EMS_CLEAR;
                        tibco.admin.PubSub.publish(msg);
                    }
                }
                this.mtxList.repaint();
            };

            servMast.prototype.tryToRemoveServiceBus = function() {
                var objCDFDocument = this.mtxList.getXML().cloneDocument();
                if(this.mtxList.getSelectedIds().length == 0) {
                    tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@@SelectNode27D"))
                }
                else {
                    var nodes = objCDFDocument.selectNodes("//record").toArray();
                    for(var i = 0; i < nodes.length; i++) {
                        if(!nodes[i].getAttribute("jsxselected") || nodes[i].getAttribute("jsxselected") == '0') {
                            objCDFDocument.getRootNode().removeChild(nodes[i]);
                        }
                    }
                    tibco.admin.Main.getServer().getCache().setDocument("emsRemoveCache27D", objCDFDocument);
                    var me = this;
                    tibco.admin.Main.confirm(
                            tibco.admin.Main.getServer().getDynamicProperty("@@deleteMess27D"),
                            tibco.admin.Main.getServer().getDynamicProperty("@@deleteConfirmMess27D"),
                            function(dialog) {
                                dialog.doClose();
                                me.callremoveServiceBusConfigurations();
                            }, null)

                }

            };

            servMast.prototype.onRefresh = function() {
                this.callgetServiceBusConfigurations();
            }

            servMast.prototype.publishSelect = function(strRECORDIDS) {
                if(this.ignoreSelects) {
                    var cacheDoc = this.mtxList.getXML();
                    if(cacheDoc) {
                        var flag = false;
                        var recordIter = cacheDoc.getXML().selectNodes("//record[@jsxselected='1']");
                        while(recordIter.hasNext()) {
                            var record = recordIter.next();
                            record.setAttribute("jsxselected", 0);
                        }
                        var iter = this.selection.iterator();
                        while(iter.hasNext()) {
                            var jsxid = iter.next();
                            var record = cacheDoc.getXML().selectSingleNode("//record[@jsxid='" + jsxid + "']");
                            if(record) {
                                flag = true;
                                record.setAttribute("jsxselected", 1);
                            }
                        }
                    }
                    this.mtxList.repaint();
                    return;
                }
                else {
                    this.multiselected = false;
                    this.selection.clear();
                    var nodes = this.mtxList.getSelectedNodes();
                    var selectionSize = nodes.size();
                    for(var i = 0; i < selectionSize; i++) {
                        this.selection.add(nodes.getItem(i).getAttribute('jsxid'));
                    }
                    if(selectionSize > 1) {
                        this.multiselected = true;
                    }
                }
                var strRowID = strRECORDIDS[0]
                this.entityId = strRowID;
                if(this.addMode) {
                    return;
                }
                var record = null;
                if(strRECORDIDS.length == 1) {
                    record = this.mtxList.getXML().selectSingleNode("//record[@jsxid='" + strRowID + "']");
                }
                if(record) {
                    this.setListButtonsEnabled(jsx3.gui.Form.STATEENABLED, true)
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.EMS_ROW_SELECTED;
                    msg.entityID = this.entityId;
                    msg.name = record.getAttribute("name");
                    msg.description = record.getAttribute("description");
                    msg.username = record.getAttribute("username");
                    msg.password = record.getAttribute("password");
                    msg.clientID = record.getAttribute("clientID");
                    msg.connectionType = record.getAttribute("subtype");
                    msg.reconnectAttemptCount = record.getAttribute("reconnectAttemptCount");
                    msg.reconnectAttemptDelay = record.getAttribute("reconnectAttemptDelay");
                    if(msg.connectionType == "JNDI") {
                        msg.JNDISharedConfigName = record.getAttribute("JNDISharedConfigName");
                        msg.topicConnectionFactory = record.getAttribute("topicConnectionFactory");
                        msg.queueConnectionFactory = record.getAttribute("queueConnectionFactory");
                    }
                    msg.providerURL = record.getAttribute("providerURL");

                    tibco.admin.PubSub.publish(msg);
                }
                else {   //this is the case when all nodes have been deselected
                    var msg = new Object();
                    msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.EMS_ROW_SELECTED;
                    msg.entityID = null;
                    this.getDescendantOfName("btn_remove_27D").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    tibco.admin.PubSub.publish(msg);
                }
            }

            servMast.prototype.setListButtonsEnabled = function(intEnabled, bRepaint) {
                var buttonPane = this.getDescendantOfName("pane_btn_27D").getDescendantsOfType(jsx3.gui.Button, false);
                for(var i = 0; i < buttonPane.length; i++) {
                    buttonPane[i].setEnabled(intEnabled, bRepaint);
                }
            }

            servMast.prototype.callremoveServiceBusConfigurations = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/enterpriseAssets/envDef/envDtls/medBus/mesSer/rules/removeServiceBusRules.xml",
                        "removeServiceBusConfigurations",
                        "remove service bus rules");
                //subscribe and call

                objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onremoveServiceBusConfigurationsSuccess);
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getEnvironmentEndpointURL());
                objService.doCall();
            };

            servMast.prototype.onremoveServiceBusConfigurationsSuccess = function(objEvent) {
                this.callgetServiceBusConfigurations();
                var msg = new Object();
                msg.subject = tibco.admin.enterpriseAssets.envDef.EventID.EMS_CLEAR;
                tibco.admin.PubSub.publish(msg);
            };

            servMast.prototype.beginDetailsEdit = function() {
                this.ignoreSelects = true;
            }

            servMast.prototype.endDetailsEdit = function() {
                this.ignoreSelects = false;
            }
        }
        );